﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Reporting.WebForms;
using PlanProd.Contracts;
using Web.Framework;
using Web.Framework.Misc;
using WebAppPlanProduccion.Code;

namespace WebAppPlanProduccion.App
{
    public partial class ReportingService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.IsPostBack)
                {
                    string reportName = this.Request.QueryString["Reporte"];

                    var reportingConfigList = BasePage.GetReportingConfiguration(this.Application);
                    var reportingConfig = reportingConfigList.Where(r => r.Nombre == reportName).Single();

                    HasPermissionToSeeMe(reportingConfig.SecurityKey);

                    ReportingConfiguration reportingConfiguration = BasePage.GetReportingConfiguration(this.Application).Where(r => r.Nombre == reportName).First();

                    this.ReportServViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                    this.ReportServViewer.ServerReport.ReportServerUrl = new Uri(reportingConfiguration.ReportUri);
                    this.ReportServViewer.ServerReport.ReportPath = reportingConfiguration.ReportPath;
                    this.ReportServViewer.ServerReport.ReportServerCredentials = new  ReportServerCredentials(reportingConfiguration.UserName, reportingConfiguration.Password, reportingConfiguration.Domain);
                    this.ReportServViewer.AsyncRendering = true;
                    this.ReportServViewer.SizeToReportContent = true;
                    this.ReportServViewer.ShowPrintButton = true;

                    var reportParameters = this.ReportServViewer.ServerReport.GetParameters();
                    if (reportParameters.Where(p => p.Name == "Empresa").Count() > 0)
                    {
                        ReportParameter reportParameterCompany = new ReportParameter("Empresa", BasePage.CompanyId.ToString());
                        this.ReportServViewer.ServerReport.SetParameters(reportParameterCompany);                        
                    }

                    if (reportParameters.Where(p => p.Name == "DestinoSecurity").Count() > 0)
                    {
                        var securityContext = GetSecurityContextForDestinos();                                               

                        List<string> destinoValues = new List<string>();

                        if (securityContext.EntityId.Count == securityContext.Total)
                        {
                            destinoValues.Add("-1");                            
                        }
                        else if (securityContext.EntityId.Count > 0)
                        {
                            foreach (int op in securityContext.EntityId)
                            {
                                destinoValues.Add(op.ToString());  
                            }
                        }
                        else
                        {
                            destinoValues.Add("-5");   // para que no muestre nada porque no tiene permisos
                        }

                        ReportParameter reportParameterDestinoSecurity = new ReportParameter("DestinoSecurity", destinoValues.ToArray());
                        //reportParameterDestinoSecurity.Visible = false;
                        this.ReportServViewer.ServerReport.SetParameters(reportParameterDestinoSecurity);                        
                    }

                    //this.ReportServViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
                    //this.ReportServViewer.ServerReport.ReportServerUrl = new Uri("http://svr-log:8081/ReportServer_RS");
                    //this.ReportServViewer.ServerReport.ReportPath = "/Paula Cahen D' Anvers/CRM/Crm.Ar.OperacionesPorPDVConLineas";
                    //this.ReportServViewer.ServerReport.ReportServerCredentials = new ReportServerCredentials("crmcontacto", "cc2011#1431", "vesuvio");
                    //this.ReportServViewer.AsyncRendering = true;
                    //this.ReportServViewer.SizeToReportContent = true;

                    //if (this.ReportServViewer.ServerReport.GetParameters().Where(p => p.Name == "companyId").Count() > 0)
                    //{
                    //    ReportParameter reportParameterCompany = new ReportParameter("companyId", BasePage.CompanyId.ToString());
                    //    this.ReportServViewer.ServerReport.SetParameters(reportParameterCompany);
                    //}
                }
            }
            catch (Exception ex)
            {
                ExceptionManager.Instance.HandleException(ex);
            }
        }
    }
}